<?php
class DataPool implements DataPool_Interface {
    /**
     * @var Container_Interface
     */
    private $_objContainer = null;


    private $_strPoolName = '';


    private $_iCurrentPoolState = 0;


    private $_uid = '';


    /**
     * @return array[]
     */
    private function _getDataAsArray () {
        if ($this->_objContainer->isExists($this->_strPoolName)) {
            return $this->_objContainer->get($this->_strPoolName);
        }

        return array();
    }


    private function _generateUid () {
        $this->_uid = md5(time()*rand());
    }


    public function __construct (Container_Interface $objContainer, $strPoolName) {
        $this->_objContainer = $objContainer;
        $this->_strPoolName  = $strPoolName;

        $arPool = $this->_getDataAsArray();

        if (!empty($arPool)) {
            $this->_uid = $arPool[self::KEY_UID];
            $this->_iCurrentPoolState = $arPool[self::KEY_STATE];
            return;
        }

        $this->_iCurrentPoolState = 0;
        $this->_generateUid();

        $this->_objContainer->set($strPoolName, array(
            self::KEY_UID => $this->_uid,
            self::KEY_STATE => $this->_iCurrentPoolState
        ));
    }


    public function isValidUid ($uid) {
        if (empty($uid) || empty($this->_uid)) {
            return false;
        }

        if ($this->_uid == $uid) {
            return true;
        }

        return false;
    }


    public function getCurrentState() {
        return $this->_iCurrentPoolState;
    }


    public function getUid () {
        return $this->_uid;
    }


    /**
     * @return Container_Interface
     */
    public function getContainer () {
        return $this->_objContainer;
    }


    public function getName () {
        return $this->_strPoolName;
    }


    public function writing ($iState, $arData) {
        if ($iState >= $this->_iCurrentPoolState) {
            // TODO error
        }

        if ($iState - $this->_iCurrentPoolState > 1) {
            // TODO error
        }

        $arPool = $this->_getDataAsArray();

        if (isset($arPool[self::KEY_DATA][$iState])) {
            $arPool[self::KEY_DATA][$iState] =
            array_merge(
                $arPool[self::KEY_DATA][$iState],
                $arData
            );
        } else {
            $arPool[self::KEY_DATA][$iState] = $arData;
        }

        $this->_iCurrentPoolState = $iState;

        $arPool[self::KEY_STATE] = $this->_iCurrentPoolState;
        $arPool[self::KEY_UID] = $this->_uid;

        $this->_objContainer->set($this->_strPoolName, $arPool);
    }


    public function clean () {
        $this->_objContainer->delete($this->_strPoolName);
        $this->_generateUid();
        $this->_iCurrentPoolState = 0;
    }


    public function getDataByState($iState) {
        $arData = $this->_getDataAsArray();
        if (isset($arData[self::KEY_DATA][$iState])) {
            return $arData[self::KEY_DATA][$iState];
        } else {
            throw new Exception('State not exists : '.$iState);
        }
    }


    public function isEmpty () {
        return !is_null($this->_objContainer->get($this->_strPoolName));
    }

}